//
//  XGL_HomeTopicBarView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/24.
//

import UIKit
import CollectionKit

protocol HomeTopicDelegate: NSObjectProtocol {
    func selectTopicItem(item: XGL_HomeTopicBarView.TopicItem)
}

class XGL_HomeTopicBarView: UIView {
    
    struct TopicItem {
        var title: String = ""
        var icon: String = ""
    }
    weak var delegate: HomeTopicDelegate?
    var collectionView: CollectionView!
    var topicDatas: ArrayDataSource<TopicItem> = ArrayDataSource(data: [])
    

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.topicDatas.data = [TopicItem(title: "Computer Games",icon: "icon_xgl_topic_type_1"),TopicItem(title: "Host Games",icon: "icon_xgl_topic_type_2"),TopicItem(title: "Handheld Games",icon: "icon_xgl_topic_type_3")]
        self.createXglView()
        self.setDatasource()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension XGL_HomeTopicBarView{
    @objc private func selectItemAction(sender: UIButton){
        let index = sender.tag - 1800
        self.delegate?.selectTopicItem(item: self.topicDatas.data(at: index))
    }
}

extension XGL_HomeTopicBarView{
    private func createXglView(){
        let headItem = UIImageView().then { make in
            make.image = imageWithName("icon_xgl_topic_head")
        }
        self.collectionView = CollectionView().then({ make in
            make.showsVerticalScrollIndicator = false
        })
        self.addSubviews([headItem,self.collectionView])
        headItem.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(15.0)
            make.width.height.equalTo(100.0)
        }
        collectionView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(headItem.snp.right).offset(8.0)
            make.height.equalTo(100.0)
            make.right.equalToSuperview()
        }
        self.snp.makeConstraints { make in
            make.height.equalTo(100.0)
        }
    }
    
    private func setDatasource(){
        let viewSource = ClosureViewSource(viewUpdater: { [weak self] (view: UIView, data: TopicItem, index: Int) in
            guard let `self` = self else { return }
            view.backgroundColor = .clear
            view.removeSubviews()
            let itembutton = UIButton().then { make in
                make.setImage(imageWithName(data.icon), for: .normal)
                make.tag  = index + 1800
                make.addTarget(self, action: #selector(self.selectItemAction(sender:)), for: .touchUpInside)
            }
            view.addSubview(itembutton)
            itembutton.snp.makeConstraints { make in
                make.center.equalToSuperview()
                make.width.height.equalTo(100.0)
            }
        })
        let sizeSource = { (index: Int, data: TopicItem, collectionSize: CGSize) -> CGSize in
            return CGSize(width: 116.0, height: 100.0)
        }
        let provider = BasicProvider(
            dataSource: topicDatas,
            viewSource: viewSource,
            sizeSource: sizeSource,
            layout: RowLayout()
        )
        collectionView.provider = provider
        provider.animator = WobbleAnimator()
    }
}
